<?php

namespace App\Models;

use App\Admin\Repositories\ReceiptInfo as AppReceiptInfo;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class ReceiptInfo extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'receipt_info';

    protected $hidden = ['deleted_at'];

    protected $fillable = [
        'user_id',
        'is_default',
        'type',
        'header',
        'company_addr',
        'company_phone',
        'bank_name',
        'bank_number',
        'tax_type',
        'tax_user_id',
        'tax_cert',
        'receive_user',
        'receive_mobile',
        'receive_addr',
        'remark',
    ];

    public function getTaxTypeTextAttribute($value)
    {
        return AppReceiptInfo::getTaxType($value);
    }

    public function getTypeTextAttribute($value)
    {
        return AppReceiptInfo::getInvoiceType($value);
    }

    /**
     * 设置默认开票信息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-06-05
     * @param [type] $id
     * @return void
     */
    public static function setDefaultReceiptInfo($id)
    {
        $receiptDefault = ReceiptInfo::where('user_id', userId())->where('is_default', '1')->first();
        if (!empty($receiptDefault)) {
            $receiptDefault->is_default = 0;
            $receiptDefault->save();
        }
        $res = ReceiptInfo::where('id', $id)->update(['is_default' => 1]);
        return $res;
    }


    public static function updatReceipt($payload, $id)
    {
        $model = self::find($id);
        // $model->act_id = $payload['act_id'];
        $model->user_id = isset($payload['user_id']) ? $payload['user_id'] : '';
        $model->type = isset($payload['type']) ? $payload['type'] : '';
        $model->header = isset($payload['header']) ? $payload['header'] : '';
        $model->company_addr = isset($payload['company_addr']) ? $payload['company_addr'] : '';
        $model->company_phone = isset($payload['company_phone']) ? $payload['company_phone'] : '';
        $model->bank_name = isset($payload['bank_name']) ? $payload['bank_name'] : '';
        $model->bank_number = isset($payload['bank_number']) ? $payload['bank_number'] : '';
        $model->tax_type = isset($payload['tax_type']) ? $payload['tax_type'] : '';
        $model->tax_user_id = isset($payload['tax_user_id']) ? $payload['tax_user_id'] : '';

        $model->tax_cert = isset($payload['tax_cert']) ? $payload['tax_cert'] : '';
        $model->receive_user = isset($payload['receive_user']) ? $payload['receive_user'] : '';
        $model->receive_mobile = isset($payload['receive_mobile']) ? $payload['receive_mobile'] : '';
        $model->receive_addr = isset($payload['receive_addr']) ? $payload['receive_addr'] : '';
        $model->remark = isset($payload['remark']) ? $payload['remark'] : '';

        return $model->save();
    }
}
